40 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 83 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 90 #define _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 95 #undef _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 102 #define _________________________________________INTERFACE_SECTION__________________________________________________________________________ 127 oC_SaveIfFalse(
"TCP::Server::New " , isaddresscorrect(ListenAddress) , oC_ErrorCode_WrongAddress )
128 && oC_SaveIfFalse(
"TCP::Server::New " ,
oC_Net_IsAddressCorrect(ListenAddress) , oC_ErrorCode_IpAddressNotCorrect )
129 && oC_SaveIfFalse(
"TCP::Server::New " , ListenAddress->
Port > 0 , oC_ErrorCode_PortNotCorrect )
131 && oC_SaveIfFalse(
"TCP::Server::New " ,
oC_Tcp_IsPortReservedBy(ListenAddress->
Port,getcurprocess()) , oC_ErrorCode_PortReservedByDifferentProcess )
132 && oC_SaveIfFalse(
"TCP::Server::New " , MaxConnections > 0 , oC_ErrorCode_MaximumValueNotCorrect )
135 server = malloc(
sizeof(
struct Server_t) + (
sizeof(
ConnectionSlot_t) * MaxConnections) , AllocationFlags_ZeroFill );
137 if( oC_SaveIfFalse(
"TCP::Server::New ", server !=
NULL , oC_ErrorCode_AllocationError) )
140 sprintf(server->
Name,
"TCP Server:%d/%d", ListenAddress->
Port, MaxConnections);
142 server->
Thread = oC_Thread_New( 0, oC_DynamicConfig_GetValue(Tcp,StackSize),
NULL, server->
Name, (oC_Thread_Function_t)
ServerThread, server );
143 server->
NewConnectionSemaphore = oC_Semaphore_New( MaxConnections, 0 , getcurallocator(), AllocationFlags_ZeroFill );
144 server->
FreeSlotsSemaphore = oC_Semaphore_New( MaxConnections, MaxConnections, getcurallocator(), AllocationFlags_ZeroFill );
147 oC_SaveIfFalse(
"TCP::Server::New ", server->
Thread !=
NULL , oC_ErrorCode_CannotCreateThread )
154 server->
Process = getcurprocess();
159 server->
ConnectionConfig.LocalWindowSize = oC_DynamicConfig_GetValue(Tcp,WindowSize);
160 server->
ConnectionConfig.LocalWindowScale = oC_DynamicConfig_GetValue(Tcp,WindowScale);
161 server->
ConnectionConfig.ConfirmationTimeout = oC_DynamicConfig_GetValue(Tcp,ConfirmationTimeout);
162 server->
ConnectionConfig.ExpirationTimeout = oC_DynamicConfig_GetValue(Tcp,ExpirationTimeout);
163 server->
ConnectionConfig.SendingAcknowledgeTimeout = oC_DynamicConfig_GetValue(Tcp,SendingAcknowledgeTimeout);
164 server->
ConnectionConfig.ReadSegmentTimeout = oC_DynamicConfig_GetValue(Tcp,ReadSegmentTimeout);
165 server->
ConnectionConfig.ReceiveTimeout = oC_DynamicConfig_GetValue(Tcp,ReceiveTimeout);
166 server->
ConnectionConfig.PacketSize = oC_DynamicConfig_GetValue(Tcp,PacketSize);
174 oC_SaveIfFalse(
"TCP::Server::New Thread " , server->
Thread ==
NULL || oC_Thread_Delete ( &server->
Thread ) , oC_ErrorCode_ReleaseError );
176 free(server, AllocationFlags_Default);
199 bool deleted =
false;
200 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
203 oC_SaveIfFalse(
"Server reference" , isram(Server) , oC_ErrorCode_AddressNotInRam )
205 && oC_SaveIfFalse(
"Timeout" , Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
208 (*Server)->ObjectControl = 0;
210 bool allConnectionsDeleted =
true;
211 bool threadDeleted =
false;
212 bool eventDeleted =
false;
213 bool event2Deleted =
false;
215 for(uint32_t i = 0; i < (*Server)->MaxConnections; i++)
217 if((*Server)->Connections[i].Connection !=
NULL)
219 allConnectionsDeleted = oC_Tcp_Connection_Delete(&((*Server)->Connections[i].Connection), oC_KTime_CalculateTimeout(endTimestamp)) && allConnectionsDeleted;
223 threadDeleted = oC_Thread_Delete(&(*Server)->Thread);
224 eventDeleted = oC_Semaphore_Delete(&(*Server)->NewConnectionSemaphore , AllocationFlags_Default);
225 event2Deleted = oC_Semaphore_Delete(&(*Server)->FreeSlotsSemaphore , AllocationFlags_Default);
228 oC_SaveIfFalse(
"Main object" , free( *Server, AllocationFlags_Default ) , oC_ErrorCode_ReleaseError )
229 && oC_SaveIfFalse(
"One of connections" , allConnectionsDeleted , oC_ErrorCode_CannotDeleteObject )
230 && oC_SaveIfFalse(
"Server Thread" , threadDeleted , oC_ErrorCode_CannotDeleteThread )
231 && oC_SaveIfFalse(
"New Connection Semaphore", eventDeleted , oC_ErrorCode_ReleaseError )
232 && oC_SaveIfFalse(
"Free Slot Semaphore" , event2Deleted , oC_ErrorCode_ReleaseError )
302 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
306 && ErrorCondition( isaddresscorrect(Config) , oC_ErrorCode_WrongAddress )
307 && ErrorCondition( Config->ConfirmationTimeout > 0 , oC_ErrorCode_TimeNotCorrect )
308 && ErrorCondition( Config->LocalAddress.
Port > 0 , oC_ErrorCode_PortNotCorrect )
309 && ErrorCondition( Config->LocalWindowSize > 0 , oC_ErrorCode_SizeNotCorrect )
311 && ErrorCondition(
oC_Tcp_IsPortReservedBy( Config->LocalAddress.
Port, getcurprocess() ) , oC_ErrorCode_PortReservedByDifferentProcess )
315 errorCode = oC_ErrorCode_None;
342 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
346 && ErrorCondition( Server->
Running ==
false , oC_ErrorCode_AlreadyRunning )
347 && ErrorCondition( oC_Thread_Run(Server->
Thread) ==
true , oC_ErrorCode_CannotRunThread )
351 errorCode = oC_ErrorCode_None;
381 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
385 && ErrorCondition( Server->
Running ==
true , oC_ErrorCode_NotRunning )
390 if( ErrorCondition( thread !=
NULL , oC_ErrorCode_AllocationError ) )
393 ErrorCondition( oC_Thread_Cancel(&Server->
Thread) , oC_ErrorCode_CannotDeleteThread )
398 errorCode = oC_ErrorCode_None;
402 oC_SaveIfFalse(
"TCP::Server::Stop ", oC_Thread_Delete(&thread), oC_ErrorCode_ReleaseError );
440 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
444 && ErrorCondition( Server->
Running ==
true , oC_ErrorCode_NotRunning )
445 && ErrorCondition( isram(outConnection) , oC_ErrorCode_OutputAddressNotInRAM )
446 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
447 && ErrorCondition( oC_Semaphore_TakeCounting( Server->
NewConnectionSemaphore, 1, Timeout ) , oC_ErrorCode_Timeout )
450 oC_IntMan_EnterCriticalSection();
452 errorCode = oC_ErrorCode_InternalDataAreDamaged;
460 errorCode = oC_ErrorCode_None;
464 oC_IntMan_ExitCriticalSection();
484 bool contains =
false;
487 oC_SaveIfFalse(
"TCP::Server::ContainsConnection - Server " ,
oC_Tcp_Server_IsCorrect(Server) , oC_ErrorCode_ObjectNotCorrect )
488 && oC_SaveIfFalse(
"TCP::Server::ContainsConnection - Connection " , oC_Tcp_Connection_IsCorrect(Connection) , oC_ErrorCode_ObjectNotCorrect )
541 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
542 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
546 && ErrorCondition( oC_Tcp_Connection_IsCorrect(Connection) , oC_ErrorCode_ObjectNotCorrect )
547 && ErrorCondition( Server->
Running ==
true , oC_ErrorCode_NotRunning )
548 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
552 if( ErrorCode( oC_Tcp_Connection_Accept( Connection, oC_KTime_CalculateTimeout(endTimestamp) ) ) )
554 errorCode = oC_ErrorCode_None;
559 oC_SaveIfFalse (
"TCP::Server::Accept ", oC_Tcp_Connection_Delete(&Connection, Timeout), oC_ErrorCode_ReleaseError );
597 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
598 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
602 && ErrorCondition( oC_Tcp_Connection_IsCorrect(Connection) , oC_ErrorCode_ObjectNotCorrect )
603 && ErrorCondition( Server->
Running ==
true , oC_ErrorCode_NotRunning )
604 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
610 && ErrorCode( oC_Tcp_Connection_Reject( Connection, oC_KTime_CalculateTimeout(endTimestamp) ) )
611 && ErrorCondition( oC_Tcp_Connection_Delete( &Connection, oC_KTime_CalculateTimeout(endTimestamp) ) , oC_ErrorCode_ReleaseError )
614 errorCode = oC_ErrorCode_None;
616 else if(oC_Tcp_Connection_IsCorrect(Connection))
618 oC_SaveIfFalse(
"TCP::Server::Reject ", oC_Tcp_Connection_Delete(&Connection, Timeout), oC_ErrorCode_ReleaseError );
648 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
652 && ErrorCondition( oC_Tcp_Connection_IsCorrect(Connection) , oC_ErrorCode_ObjectNotCorrect )
654 && ErrorCondition( oC_Semaphore_TakeCounting(Server->
FreeSlotsSemaphore,1,Timeout) , oC_ErrorCode_NoFreeSlots )
659 oC_IntMan_EnterCriticalSection();
668 oC_IntMan_ExitCriticalSection();
670 if( ErrorCondition( freeIndex < Server->
MaxConnections , oC_ErrorCode_NoFreeSlots ) )
675 errorCode = oC_ErrorCode_None;
707 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
711 && ErrorCondition( oC_Tcp_Connection_IsCorrect(Connection) , oC_ErrorCode_ObjectNotCorrect )
714 errorCode = oC_ErrorCode_ObjectNotFoundOnList;
716 oC_IntMan_EnterCriticalSection();
728 errorCode = oC_ErrorCode_None;
732 oC_IntMan_ExitCriticalSection();
791 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
795 && ErrorCondition( isaddresscorrect(Function) , oC_ErrorCode_WrongAddress )
800 errorCode = oC_ErrorCode_None;
806 #undef _________________________________________INTERFACE_SECTION__________________________________________________________________________ 813 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ 823 oC_SaveIfFalse(
"TCP::Server::ConnectionFinished (Connection)", oC_Tcp_Connection_IsCorrect(Connection), oC_ErrorCode_ObjectNotCorrect )
824 && oC_SaveIfFalse(
"TCP::Server::ConnectionFinished (Server)" ,
oC_Tcp_Server_IsCorrect(Server) , oC_ErrorCode_ObjectNotCorrect )
832 oC_SaveIfFalse (
"TCP::Server::ConnectionFinished ", oC_Tcp_Connection_Delete(&Connection,
s(1)), oC_ErrorCode_ReleaseError );
844 uint32_t connectionId = 0;
849 connectionId = connectionId << 1;
851 if(connection ==
NULL)
856 if(oC_SaveIfFalse(
"TCP::Server::ServerThread ", connection !=
NULL, oC_ErrorCode_AllocationError))
859 oC_SaveIfErrorOccur(
"TCP::Server::ServerThread Wait for connection - ", oC_Tcp_Connection_WaitForConnection(connection, oC_DynamicConfig_GetValue( Tcp, WaitForConnectionTimeout )))
860 && oC_SaveIfErrorOccur(
"TCP::Server::ServerThread " ,
oC_Tcp_Server_AddConnection(Server, connection, oC_DynamicConfig_GetValue( Tcp, ExpirationTimeout )))
864 char addressString[30] = {0};
866 oC_Tcp_Connection_ReadRemote(connection,&address);
869 kdebuglog(oC_LogType_Track,
"TCP::Server:%d - received new connection from %s\n", Server->
ConnectionConfig.LocalAddress.
Port, addressString);
874 if(oC_Tcp_Connection_IsCorrect(connection))
878 oC_SaveIfErrorOccur(
"TCP::Server::ServerThread - rejecting -", oC_Tcp_Connection_Reject(connection, oC_DynamicConfig_GetValue( Tcp, RejectConnectionTimeout )));
880 oC_Tcp_Connection_Delete(&connection, oC_DynamicConfig_GetValue(Tcp,DeleteConnectionTimeout));
892 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ oC_Tcp_Connection_Config_t ConnectionConfig
Configuration of connections of the server.
oC_Tcp_Server_t oC_Tcp_Server_New(const oC_Net_Address_t *ListenAddress, uint32_t MaxConnections)
creates TCP server
#define s(time)
Number of s.
oC_Tcp_ConnectionFinishedFunction_t ConnFinishedFunction_t
redefinition of type for storing pointer to the connection finished function
oC_Process_t oC_Tcp_Server_GetProcess(oC_Tcp_Server_t Server)
returns process associated with the server
oC_ErrorCode_t oC_Tcp_Server_Run(oC_Tcp_Server_t Server)
starts the TCP server
static void ConnectionFinished(oC_Tcp_Connection_t Connection, oC_Tcp_Server_t Server)
function called, when the connection has been finished
stores TCP connection data
bool oC_Tcp_Server_ContainsConnection(oC_Tcp_Server_t Server, oC_Tcp_Connection_t Connection)
checks if the server contains the connection
oC_Semaphore_t NewConnectionSemaphore
Semaphore that counts new connections - connections that are currently not handled.
The file with interface for thread managing.
oC_Thread_t Thread
Thread for handling the server.
oC_ErrorCode_t oC_Tcp_Server_RemoveConnection(oC_Tcp_Server_t Server, oC_Tcp_Connection_t Connection)
removes connection from server's connections list
oC_ErrorCode_t oC_Tcp_Server_AcceptConnection(oC_Tcp_Server_t Server, oC_Tcp_Connection_t Connection, oC_Time_t Timeout)
accepts TCP connection
oC_Tcp_Connection_t Connection
Connection object or NULL if it is not used.
bool Handled
true if the slot is already handled by an application
bool oC_Tcp_Server_IsRunning(oC_Tcp_Server_t Server)
checks if the TCP server is already running
oC_Tcp_Connection_t oC_Tcp_Connection_New(const oC_Tcp_Connection_Config_t *Config)
allocates memory for a new TCP connection object
oC_Tcp_Port_t
stores TCP port
oC_Process_t Process
Process assiociated with the server.
stores data for handling connections array
bool oC_Tcp_IsPortReservedBy(oC_Tcp_Port_t Port, oC_Process_t Process)
checks if the port is reserved by the given process
The file with helper macros for managing objects.
bool oC_Tcp_IsPortReserved(oC_Tcp_Port_t Port)
checks if the port is reserved
oC_ErrorCode_t oC_Tcp_Server_AddConnection(oC_Tcp_Server_t Server, oC_Tcp_Connection_t Connection, oC_Time_t Timeout)
adds connection to server's connections list
uint32_t MaxConnections
Maximum number of connections that server can handle.
uint32_t oC_ObjectControl_t
stores object control value
oC_ErrorCode_t oC_Tcp_Server_WaitForConnection(oC_Tcp_Server_t Server, oC_Tcp_Connection_t *outConnection, oC_Time_t Timeout)
Waits for new TCP connection.
void * ConnectionFinishedParameter
Parameter to give to the connection finished function.
oC_Semaphore_t FreeSlotsSemaphore
Semaphore that counts free slots for connections.
static oC_ErrorCode_t oC_Net_AddressToString(const oC_Net_Address_t *Address, char *outString, oC_MemorySize_t Size)
prints IP to the string
static void ServerThread(oC_Tcp_Server_t Server)
main thread of TCP servers
The file with interface for interrupt manager.
ConnectionSlot_t Connections[1]
Array with TCP connections slots.
Handles configuration of the Dynamic.
oC_ObjectControl_t ObjectControl
Special field for storing magic cookie that helps to recognize correct TCP server object...
oC_Tcp_Port_t oC_Tcp_Server_GetPort(oC_Tcp_Server_t Server)
returns local port of the server
static oC_ObjectControl_t oC_CountObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId)
counts object control for object
static bool oC_CheckObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId, oC_ObjectControl_t ObjectControl)
checks if object control is correct
static bool oC_Net_IsAddressCorrect(const oC_Net_Address_t *Address)
returns true if the given address is correct
oC_ErrorCode_t oC_Tcp_Server_SetConnectionConfig(oC_Tcp_Server_t Server, const oC_Tcp_Connection_Config_t *Config)
sets connection configuration
bool oC_Tcp_Server_IsCorrect(oC_Tcp_Server_t Server)
checks if the TCP server object is correct
oC_ErrorCode_t oC_Tcp_Server_Stop(oC_Tcp_Server_t Server)
stops the TCP server
The file with interface for semaphores.
oC_ErrorCode_t oC_Tcp_Server_SetConnectionFinished(oC_Tcp_Server_t Server, oC_Tcp_ConnectionFinishedFunction_t Function, void *Parameter)
sets a pointer for 'connection finished function'
char Name[30]
Name of the TCP server.
bool Running
The flag is set to true if the server is already running.
oC_Net_AddressType_t Type
Type of the address stored inside.
oC_ErrorCode_t oC_Tcp_Server_RejectConnection(oC_Tcp_Server_t Server, oC_Tcp_Connection_t Connection, oC_Time_t Timeout)
rejects TCP connection
oC_Net_Port_t Port
Port of the address.
ConnFinishedFunction_t ConnectionFinishedFunction
Pointer to the function to call when the connection has finished.
bool oC_Tcp_Server_Delete(oC_Tcp_Server_t *Server, oC_Time_t Timeout)
deletes TCP server
The file with standard input/output operations.
#define NULL
pointer to a zero