Choco OS  V.0.16.9.0
Join to the chocolate world
oc_tcp.c
Go to the documentation of this file.
1 
27 #include <oc_tcp.h>
28 #include <oc_module.h>
29 #include <oc_portman.h>
30 #include <oc_list.h>
31 #include <oc_mutex.h>
32 #include <oc_dynamic_config.h>
33 
39 #define _________________________________________TYPES_SECTION______________________________________________________________________________
40 
41 
42 #undef _________________________________________TYPES_SECTION______________________________________________________________________________
43 
49 #define _________________________________________PROTOTYPES_SECTION_________________________________________________________________________
50 
54 static oC_ErrorCode_t CloseServersCreatedBy ( oC_Process_t Process , oC_Time_t Timeout );
55 static oC_ErrorCode_t CloseConnectionsCreatedBy ( oC_Process_t Process , oC_Time_t Timeout );
56 
57 #undef _________________________________________PROTOTYPES_SECTION_________________________________________________________________________
58 
59 
65 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________
66 
67 static oC_List(oC_Tcp_Server_t) Servers = NULL;
68 static oC_List(oC_Tcp_Connection_t) Connections = NULL;
69 static oC_Mutex_t ModuleBusy = NULL;
70 static const oC_Allocator_t Allocator = {
71  .Name = "TCP"
72 };
73 static const oC_PortMan_Config_t PortManConfig = {
75  .FirstDynamicPortNumber = oC_Tcp_Port_NumberOfSpecialPorts ,
76  .LastDynamicPortNumber = oC_uint16_MAX
77 };
78 
79 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________
80 
81 
87 #define _________________________________________FUNCTIONS_SECTION__________________________________________________________________________
88 
89 //==========================================================================================================================================
117 //==========================================================================================================================================
118 oC_ErrorCode_t oC_Tcp_TurnOn( oC_Time_t Timeout )
119 {
120  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
121 
122  if(oC_Module_TurnOffVerification(&errorCode, oC_Module_Tcp))
123  {
124  if(
125  ErrorCondition( oC_Module_IsTurnedOn(oC_Module_PortMan) , oC_ErrorCode_RequiredModuleNotEnabled )
126  && ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied )
127  && ErrorCode ( oC_PortMan_RegisterModule(oC_Module_Tcp, &PortManConfig, Timeout) )
128  )
129  {
130  Servers = oC_List_New(&Allocator, AllocationFlags_Default);
131  Connections = oC_List_New(&Allocator, AllocationFlags_Default);
132  ModuleBusy = oC_Mutex_New( oC_Mutex_Type_Recursive, &Allocator, AllocationFlags_Default );
133 
134  if(
135  ErrorCondition( Servers != NULL , oC_ErrorCode_AllocationError )
136  && ErrorCondition( Connections != NULL , oC_ErrorCode_AllocationError )
137  && ErrorCondition( ModuleBusy != NULL , oC_ErrorCode_AllocationError )
138  )
139  {
140  oC_Module_TurnOn(oC_Module_Tcp);
141  errorCode = oC_ErrorCode_None;
142  }
143  else
144  {
145  bool listServerListDeleted = Servers == NULL || oC_List_Delete ( Servers , AllocationFlags_Default );
146  bool listConnectionsListDeleted = Connections == NULL || oC_List_Delete ( Connections , AllocationFlags_Default );
147  bool mutexDeleted = ModuleBusy == NULL || oC_Mutex_Delete( &ModuleBusy , AllocationFlags_Default );
148 
149  oC_SaveIfFalse( "TCP::TurnOn ", listServerListDeleted && listConnectionsListDeleted && mutexDeleted , oC_ErrorCode_ReleaseError );
150  }
151  }
152  }
153 
154  return errorCode;
155 }
156 
157 //==========================================================================================================================================
184 //==========================================================================================================================================
185 oC_ErrorCode_t oC_Tcp_TurnOff( oC_Time_t Timeout )
186 {
187  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
188  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
189 
190  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_Tcp))
191  {
192  if(
193  ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
194  && ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied )
195  && ErrorCondition( oC_Mutex_Take(ModuleBusy, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
196  && ErrorCode ( oC_PortMan_UnregisterModule(oC_Module_Tcp, oC_KTime_CalculateTimeout(endTimestamp)) )
197  )
198  {
199  oC_Module_TurnOff(oC_Module_Tcp);
200 
201  bool allServersDeleted = true;
202  bool allConnectionsDeleted = true;
203 
204  foreach(Servers,server)
205  {
206  allServersDeleted = oC_Tcp_Server_Delete( &server, oC_KTime_CalculateTimeout(endTimestamp) ) && allServersDeleted;
207  }
208 
209  foreach(Connections,connection)
210  {
211  allConnectionsDeleted = oC_Tcp_Connection_Delete(&connection, oC_KTime_CalculateTimeout(endTimestamp)) && allConnectionsDeleted;
212  }
213 
214  bool serversListDeleted = oC_List_Delete ( Servers , AllocationFlags_Default );
215  bool connectionsListDeleted = oC_List_Delete ( Connections , AllocationFlags_Default );
216  bool mutexDeleted = oC_Mutex_Delete( &ModuleBusy , AllocationFlags_Default );
217 
218  if(
219  ErrorCondition( serversListDeleted
220  && connectionsListDeleted
221  && allServersDeleted
222  && allConnectionsDeleted
223  && mutexDeleted , oC_ErrorCode_ReleaseError)
224  )
225  {
226  errorCode = oC_ErrorCode_None;
227  }
228  }
229  }
230 
231  return errorCode;
232 }
233 
234 //==========================================================================================================================================
260 //==========================================================================================================================================
261 oC_ErrorCode_t oC_Tcp_CloseProcess( oC_Process_t Process , oC_Time_t Timeout )
262 {
263  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
264  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
265 
266  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_Tcp))
267  {
268  if(
269  ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
270  && ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied )
271  && ErrorCondition( oC_Mutex_Take(ModuleBusy, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
272  )
273  {
274  if(
275  ErrorCode( CloseServersCreatedBy ( Process, oC_KTime_CalculateTimeout(endTimestamp)) )
276  && ErrorCode( CloseConnectionsCreatedBy ( Process, oC_KTime_CalculateTimeout(endTimestamp)) )
277  && ErrorCode( oC_PortMan_ReleaseAllPortsOf ( oC_Module_Tcp, Process, oC_KTime_CalculateTimeout(endTimestamp)) )
278  )
279  {
280  errorCode = oC_ErrorCode_None;
281  }
282  oC_Mutex_Give(ModuleBusy);
283  }
284  }
285 
286  return errorCode;
287 }
288 
289 //==========================================================================================================================================
318 //==========================================================================================================================================
319 oC_ErrorCode_t oC_Tcp_ReservePort( oC_Tcp_Port_t * Port , oC_Time_t Timeout )
320 {
321  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
322 
323  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_Tcp))
324  {
325  if(
326  ErrorCondition( isram(Port) , oC_ErrorCode_AddressNotInRam )
327  && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
328  )
329  {
330  oC_PortMan_Port_t port = (oC_PortMan_Port_t)*Port;
331 
332  if(ErrorCode( oC_PortMan_ReservePort(oC_Module_Tcp, &port, Timeout) ))
333  {
334  *Port = port;
335  errorCode = oC_ErrorCode_None;
336  }
337  }
338  }
339 
340  return errorCode;
341 }
342 
343 //==========================================================================================================================================
370 //==========================================================================================================================================
371 oC_ErrorCode_t oC_Tcp_ReleasePort( oC_Tcp_Port_t Port , oC_Time_t Timeout )
372 {
373  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
374 
375  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_Tcp))
376  {
378 
379  if(
380  ErrorCondition( port > 0 , oC_ErrorCode_PortNotCorrect )
381  && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
382  )
383  {
384  errorCode = oC_PortMan_ReleasePort(oC_Module_Tcp, Port, Timeout);
385  }
386  }
387 
388  return errorCode;
389 }
390 
391 //==========================================================================================================================================
395 //==========================================================================================================================================
397 {
398  return oC_PortMan_IsPortReserved(oC_Module_Tcp , Port);
399 }
400 
401 //==========================================================================================================================================
405 //==========================================================================================================================================
407 {
408  return oC_PortMan_IsPortReservedBy(oC_Module_Tcp, Port, Process);
409 }
410 
411 //==========================================================================================================================================
442 //==========================================================================================================================================
443 oC_ErrorCode_t oC_Tcp_Connect( const oC_Net_Address_t * Destination , oC_Tcp_Port_t LocalPort , oC_Tcp_Connection_t * outConnection , oC_Time_t Timeout )
444 {
445  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
446  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
447 
448  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_Tcp))
449  {
450  if(
451  ErrorCondition( isaddresscorrect(Destination) , oC_ErrorCode_WrongAddress )
452  && ErrorCondition( oC_Net_IsAddressCorrect(Destination) , oC_ErrorCode_IpAddressNotCorrect )
453  && ErrorCondition( Destination->Port > 0 , oC_ErrorCode_PortNotCorrect )
454  && ErrorCondition( isram(outConnection) , oC_ErrorCode_OutputAddressNotInRAM )
455  && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
456  && ErrorCode ( oC_Tcp_ReservePort( &LocalPort, oC_KTime_CalculateTimeout(endTimestamp) ) )
457  && ErrorCondition( oC_Mutex_Take(ModuleBusy,oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
458  )
459  {
460  oC_Tcp_Connection_Config_t config = {
461  .InitialSequenceNumber = oC_DynamicConfig_GetValue( Tcp, InitialSequenceNumber ) ,
462  .InitialAcknowledgeNumber = 0 ,
463  .LocalWindowSize = oC_DynamicConfig_GetValue( Tcp, WindowSize ) ,
464  .LocalWindowScale = oC_DynamicConfig_GetValue( Tcp, WindowScale ) ,
465  .ConfirmationTimeout = oC_DynamicConfig_GetValue( Tcp, ConfirmationTimeout ) ,
466  .LocalAddress.Type = Destination->Type ,
467  .LocalAddress.IPv6.HighPart = 0 ,
468  .LocalAddress.IPv6.LowPart = 0 ,
469  .LocalAddress.Port = LocalPort ,
470  .LocalAddress.Protocol = oC_Net_Protocol_TCP ,
471  };
472  oC_Tcp_Connection_t connection = oC_Tcp_Connection_New( &config );
473  bool added = oC_List_PushBack( Connections, connection, &Allocator );
474 
475  if(
476  ErrorCondition( connection != NULL , oC_ErrorCode_AllocationError )
477  && ErrorCondition( added , oC_ErrorCode_CannotAddObjectToList )
478  && ErrorCode ( oC_Tcp_Connection_Connect(connection, oC_KTime_CalculateTimeout(endTimestamp)) )
479  )
480  {
481  *outConnection = connection;
482  errorCode = oC_ErrorCode_None;
483  }
484  else
485  {
486  bool removedFromList = added == false || oC_List_RemoveAll( Connections, connection );
487  bool deleted = connection == NULL || oC_Tcp_Connection_Delete( &connection, oC_KTime_CalculateTimeout(endTimestamp) );
488 
489  oC_SaveIfFalse ("Tcp::Connect ", removedFromList , oC_ErrorCode_CannotRemoveObjectFromList );
490  oC_SaveIfFalse ("Tcp::Connect ", deleted , oC_ErrorCode_ReleaseError );
491  oC_SaveIfErrorOccur("Tcp::Connect ", oC_Tcp_ReleasePort(LocalPort, oC_KTime_CalculateTimeout(endTimestamp)) );
492  }
493 
494  oC_Mutex_Give(ModuleBusy);
495  }
496  }
497 
498  return errorCode;
499 }
500 
501 //==========================================================================================================================================
531 //==========================================================================================================================================
532 oC_ErrorCode_t oC_Tcp_Disconnect( oC_Tcp_Connection_t * Connection , oC_Time_t Timeout )
533 {
534  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
535  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
536 
537  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_Tcp))
538  {
539  if(
540  ErrorCondition( isram(Connection) , oC_ErrorCode_AddressNotInRam )
541  && ErrorCondition( oC_Tcp_Connection_IsCorrect(*Connection) , oC_ErrorCode_ObjectNotCorrect )
542  && ErrorCondition( oC_Tcp_Connection_IsConnected(*Connection) , oC_ErrorCode_NotConnected )
543  && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
544  && ErrorCondition( oC_Mutex_Take(ModuleBusy, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
545  )
546  {
547  oC_Tcp_Connection_t connection = *Connection;
548  bool foundOnList = oC_List_Contains( Connections, *Connection );
549  oC_Tcp_Server_t server = GetServerWithConnection(connection);
550  oC_Net_Address_t localAddress;
551 
552  bzero(&localAddress,sizeof(localAddress));
553 
554  if(ErrorCondition( foundOnList || server != NULL, oC_ErrorCode_ObjectNotFoundOnList ))
555  {
556  errorCode = oC_ErrorCode_None;
557  *Connection = NULL;
558 
559  bool removedFromList = foundOnList == false || oC_List_RemoveAll( Connections, connection );
560 
561  server == NULL
562  || ErrorCode( oC_Tcp_Server_RemoveConnection( server, connection ) );
563 
564  ErrorCondition( removedFromList , oC_ErrorCode_CannotRemoveObjectFromList );
565  ErrorCode( oC_Tcp_Connection_ReadLocal( connection, &localAddress) )
566  && ErrorCode( oC_Tcp_ReleasePort( localAddress.Port , oC_KTime_CalculateTimeout(endTimestamp) ) );
567 
568  ErrorCode ( oC_Tcp_Connection_Disconnect( connection, oC_KTime_CalculateTimeout(endTimestamp)) );
569  ErrorCondition( oC_Tcp_Connection_Delete ( Connection, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ReleaseError );
570  }
571 
572 
573  oC_Mutex_Give(ModuleBusy);
574  }
575  }
576 
577  return errorCode;
578 }
579 
580 //==========================================================================================================================================
614 //==========================================================================================================================================
615 oC_ErrorCode_t oC_Tcp_Listen( const oC_Net_Address_t * Source , oC_Tcp_Server_t * outServer , uint32_t MaxConnections , oC_Time_t Timeout )
616 {
617  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
618  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
619 
620  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_Tcp))
621  {
622  oC_Tcp_Port_t port = 0;
623 
624  if(
625  ErrorCondition( isaddresscorrect(Source) , oC_ErrorCode_WrongAddress )
626  && ErrorCondition( oC_Net_IsAddressCorrect(Source) , oC_ErrorCode_IpAddressNotCorrect )
627  && ErrorCondition( (port = Source->Port) > 0 , oC_ErrorCode_PortNotCorrect )
628  && ErrorCondition( isram(outServer) , oC_ErrorCode_OutputAddressNotInRAM )
629  && ErrorCondition( MaxConnections > 0 , oC_ErrorCode_MaximumValueNotCorrect )
630  && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
631  && ErrorCode ( oC_Tcp_ReservePort( &port, oC_KTime_CalculateTimeout(endTimestamp)) )
632  && ErrorCondition( oC_Mutex_Take(ModuleBusy , oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
633  )
634  {
635  oC_Tcp_Server_t server = oC_Tcp_Server_New( Source, MaxConnections );
636  bool pushed = oC_List_PushBack( Servers, server, &Allocator );
637 
638  if(
639  ErrorCondition( server != NULL , oC_ErrorCode_AllocationError )
640  && ErrorCondition( pushed , oC_ErrorCode_CannotAddObjectToList )
641  && ErrorCode ( oC_Tcp_Server_Run(server) )
642  )
643  {
644  *outServer = server;
645  errorCode = oC_ErrorCode_None;
646  }
647  else
648  {
649  bool removedFromList = pushed == false || oC_List_RemoveAll( Servers, server );
650  bool deleted = server == NULL || oC_Tcp_Server_Delete( &server, oC_KTime_CalculateTimeout(endTimestamp) );
651 
652  oC_SaveIfFalse ( "TCP::Listen ", removedFromList , oC_ErrorCode_CannotRemoveObjectFromList );
653  oC_SaveIfFalse ( "TCP::Listen ", deleted , oC_ErrorCode_ReleaseError );
654  oC_SaveIfErrorOccur ( "TCP::Listen ", oC_Tcp_ReleasePort(port, oC_KTime_CalculateTimeout(endTimestamp)) );
655  }
656  oC_Mutex_Give(ModuleBusy);
657  }
658  }
659 
660  return errorCode;
661 }
662 
663 //==========================================================================================================================================
692 //==========================================================================================================================================
693 oC_ErrorCode_t oC_Tcp_StopListen( oC_Tcp_Server_t * Server , oC_Time_t Timeout )
694 {
695  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
696  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
697 
698  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_Tcp))
699  {
700  if(
701  ErrorCondition( isram(Server) , oC_ErrorCode_AddressNotInRam )
702  && ErrorCondition( oC_Tcp_Server_IsCorrect(*Server) , oC_ErrorCode_ObjectNotCorrect )
703  && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
704  && ErrorCondition( oC_Tcp_Server_IsRunning(*Server) , oC_ErrorCode_ServerNotStarted )
705  && ErrorCondition( oC_Mutex_Take(ModuleBusy , oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
706  )
707  {
708  oC_Tcp_Port_t port = oC_Tcp_Server_GetPort(*Server);
709  bool foundOnList = oC_List_Contains(Servers,*Server);
710 
711  if( ErrorCondition( foundOnList , oC_ErrorCode_ObjectNotFoundOnList ) )
712  {
713  errorCode = oC_ErrorCode_None;
714 
715  ErrorCode( oC_Tcp_Server_Stop(*Server) );
716  ErrorCode( oC_PortMan_ReleasePort(oC_Module_Tcp, port, oC_KTime_CalculateTimeout(endTimestamp)) );
717 
718  bool removedFromList = oC_List_RemoveAll(Servers, *Server);
719 
720  ErrorCondition ( oC_Tcp_Server_Delete(Server, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ReleaseError );
721  ErrorCondition ( removedFromList , oC_ErrorCode_CannotRemoveObjectFromList );
722  }
723 
724  oC_Mutex_Give(ModuleBusy);
725  }
726  }
727 
728  return errorCode;
729 }
730 
731 //==========================================================================================================================================
758 //==========================================================================================================================================
759 oC_ErrorCode_t oC_Tcp_Accept( oC_Tcp_Server_t Server , oC_Tcp_Connection_t * outConnection , oC_Time_t Timeout )
760 {
761  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
762  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
763 
764  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_Tcp))
765  {
766  if(
767  ErrorCondition( oC_Tcp_Server_IsCorrect(Server) , oC_ErrorCode_ObjectNotCorrect )
768  && ErrorCondition( isram(outConnection) , oC_ErrorCode_OutputAddressNotInRAM )
769  && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
770  && ErrorCondition( oC_Tcp_Server_IsRunning(Server) , oC_ErrorCode_ServerNotStarted )
771  && ErrorCondition( oC_Mutex_Take(ModuleBusy , oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
772  )
773  {
774  oC_Mutex_Give(ModuleBusy);
775 
776  while(
777  ErrorCode( oC_Tcp_Server_WaitForConnection(Server, outConnection, oC_KTime_CalculateTimeout(endTimestamp)) )
778  )
779  {
780  if(ErrorCode( oC_Tcp_Server_AcceptConnection(Server, *outConnection, oC_KTime_CalculateTimeout(endTimestamp)) ))
781  {
782  errorCode = oC_ErrorCode_None;
783  break;
784  }
785  }
786  }
787  }
788 
789  return errorCode;
790 }
791 
792 //==========================================================================================================================================
818 //==========================================================================================================================================
819 oC_ErrorCode_t oC_Tcp_Send( oC_Tcp_Connection_t Connection, const void * Buffer, oC_MemorySize_t Size, oC_Time_t Timeout )
820 {
821  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
822 
823  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_Tcp))
824  {
825  errorCode = oC_Tcp_Connection_Send(Connection, Buffer, Size, Timeout);
826  }
827 
828  return errorCode;
829 }
830 
831 //==========================================================================================================================================
857 //==========================================================================================================================================
858 oC_ErrorCode_t oC_Tcp_Receive( oC_Tcp_Connection_t Connection, void * outBuffer, oC_MemorySize_t Size, oC_Time_t Timeout )
859 {
860  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
861 
862  if(oC_Module_TurnOnVerification(&errorCode, oC_Module_Tcp))
863  {
864  errorCode = oC_Tcp_Connection_Receive(Connection, outBuffer, Size, Timeout);
865  }
866 
867  return errorCode;
868 }
869 
870 #undef _________________________________________FUNCTIONS_SECTION__________________________________________________________________________
871 
877 #define _________________________________________LOCALS_SECTION_____________________________________________________________________________
878 
879 //==========================================================================================================================================
883 //==========================================================================================================================================
885 {
886  oC_Tcp_Server_t foundServer = NULL;
887 
888  foreach(Servers,server)
889  {
890  if(oC_Tcp_Server_ContainsConnection(server,Connection))
891  {
892  foundServer = server;
893  break;
894  }
895  }
896 
897  return foundServer;
898 }
899 
900 //==========================================================================================================================================
904 //==========================================================================================================================================
906 {
907  oC_Tcp_Server_t foundServer = NULL;
908 
909  foreach(Servers, server)
910  {
911  if(oC_Tcp_Server_GetProcess(server) == Process)
912  {
913  foundServer = server;
914  break;
915  }
916  }
917 
918  return foundServer;
919 }
920 
921 //==========================================================================================================================================
925 //==========================================================================================================================================
927 {
928  oC_Tcp_Connection_t foundConnection = NULL;
929 
930  foreach(Connections,connection)
931  {
932  if(oC_Tcp_Connection_GetProcess(connection) == Process)
933  {
934  foundConnection = connection;
935  break;
936  }
937  }
938 
939  return foundConnection;
940 }
941 
942 //==========================================================================================================================================
946 //==========================================================================================================================================
947 static oC_ErrorCode_t CloseServersCreatedBy( oC_Process_t Process , oC_Time_t Timeout )
948 {
949  oC_ErrorCode_t errorCode = oC_ErrorCode_None;
950  oC_Tcp_Server_t server = NULL;
951  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
952 
953  while( (server = GetServerCreatedBy(Process)) != NULL )
954  {
955  if(oC_Tcp_Server_IsRunning(server))
956  {
957  ErrorCode( oC_Tcp_Server_Stop(server) );
958  }
959  bool removedFromList = oC_List_RemoveAll(Servers,server);
960 
961  ErrorCondition( removedFromList , oC_ErrorCode_CannotRemoveObjectFromList );
962  ErrorCondition( oC_Tcp_Server_Delete( &server, oC_KTime_CalculateTimeout(endTimestamp) ), oC_ErrorCode_ReleaseError );
963  }
964 
965  return errorCode;
966 }
967 
968 //==========================================================================================================================================
972 //==========================================================================================================================================
973 static oC_ErrorCode_t CloseConnectionsCreatedBy( oC_Process_t Process , oC_Time_t Timeout )
974 {
975  oC_ErrorCode_t errorCode = oC_ErrorCode_None;
976  oC_Tcp_Connection_t connection = NULL;
977  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
978 
979  while( (connection = GetConnectionCreatedBy(Process)) != NULL )
980  {
981  if(oC_Tcp_Connection_IsConnected(connection))
982  {
983  ErrorCode( oC_Tcp_Connection_Disconnect(connection, oC_KTime_CalculateTimeout(endTimestamp)) );
984  }
985  bool removedFromList = oC_List_RemoveAll(Connections,connection);
986 
987  ErrorCondition( removedFromList , oC_ErrorCode_CannotRemoveObjectFromList );
988  ErrorCondition( oC_Tcp_Connection_Delete( &connection, oC_KTime_CalculateTimeout(endTimestamp) ), oC_ErrorCode_ReleaseError );
989  }
990 
991  return errorCode;
992 }
993 
994 #undef _________________________________________LOCALS_SECTION_____________________________________________________________________________
995 
oC_ErrorCode_t oC_Tcp_StopListen(oC_Tcp_Server_t *Server, oC_Time_t Timeout)
stops listen at the given TCP port
Definition: oc_tcp.c:693
uint32_t oC_PortMan_Port_t
stores the port number
Definition: oc_portman.h:54
oC_Tcp_Server_t oC_Tcp_Server_New(const oC_Net_Address_t *ListenAddress, uint32_t MaxConnections)
creates TCP server
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
const char * Name
Definition: oc_stdlib.h:161
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
static oC_Tcp_Connection_t GetConnectionCreatedBy(oC_Process_t Process)
returns first connection that has been created by the given process
Definition: oc_tcp.c:926
identifier for allocations
Definition: oc_stdlib.h:159
oC_ErrorCode_t oC_PortMan_RegisterModule(oC_Module_t Module, const oC_PortMan_Config_t *Config, oC_Time_t Timeout)
registers module in the port manager
Definition: oc_portman.c:257
oC_ErrorCode_t oC_Tcp_Server_RemoveConnection(oC_Tcp_Server_t Server, oC_Tcp_Connection_t Connection)
removes connection from server&#39;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
static oC_Tcp_Server_t GetServerCreatedBy(oC_Process_t Process)
returns first server that has been created by the given process
Definition: oc_tcp.c:905
oC_ErrorCode_t oC_Tcp_TurnOn(oC_Time_t Timeout)
turns on TCP module
Definition: oc_tcp.c:118
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_ErrorCode_t oC_Tcp_Disconnect(oC_Tcp_Connection_t *Connection, oC_Time_t Timeout)
disconnects TCP connection
Definition: oc_tcp.c:532
stores network address
Definition: oc_net.h:441
oC_Tcp_Port_t
stores TCP port
Definition: oc_tcp.h:75
Module for managing port reservations.
oC_ErrorCode_t oC_Tcp_ReservePort(oC_Tcp_Port_t *Port, oC_Time_t Timeout)
reserves TCP port
Definition: oc_tcp.c:319
Transmission Control Protocol.
Definition: oc_net.h:159
oC_ErrorCode_t oC_PortMan_ReleaseAllPortsOf(oC_Module_t Module, oC_Process_t Process, oC_Time_t Timeout)
releases all ports reserved by a given process
Definition: oc_portman.c:522
oC_ErrorCode_t oC_Tcp_Accept(oC_Tcp_Server_t Server, oC_Tcp_Connection_t *outConnection, oC_Time_t Timeout)
waits for new connection and accepts it
Definition: oc_tcp.c:759
bool oC_Tcp_IsPortReservedBy(oC_Tcp_Port_t Port, oC_Process_t Process)
checks if the port is reserved by the given process
Definition: oc_tcp.c:406
bool oC_Tcp_IsPortReserved(oC_Tcp_Port_t Port)
checks if the port is reserved
Definition: oc_tcp.c:396
The file with interface for the module library.
static bool oC_Module_IsTurnedOn(oC_Module_t Module)
checks if the module is turned on
Definition: oc_module.h:121
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.
oC_ErrorCode_t oC_Tcp_CloseProcess(oC_Process_t Process, oC_Time_t Timeout)
closes all objects related with the process
Definition: oc_tcp.c:261
oC_PortMan_Port_t MaximumPortNumber
Maximum number of correct port.
Definition: oc_portman.h:65
The file with list library.
Handles configuration of the Dynamic.
static oC_Tcp_Server_t GetServerWithConnection(oC_Tcp_Connection_t Connection)
searches for a server that contains the given connection
Definition: oc_tcp.c:884
oC_Tcp_Port_t oC_Tcp_Server_GetPort(oC_Tcp_Server_t Server)
returns local port of the server
static bool oC_Net_IsAddressCorrect(const oC_Net_Address_t *Address)
returns true if the given address is correct
Definition: oc_net.h:918
The file with interface for mutex managing.
static void oC_Module_TurnOn(oC_Module_t Module)
sets module as turned on
Definition: oc_module.h:170
bool oC_PortMan_IsPortReservedBy(oC_Module_t Module, oC_PortMan_Port_t Port, oC_Process_t Process)
checks if the given port is reserved by the given process
Definition: oc_portman.c:602
bool oC_PortMan_IsPortReserved(oC_Module_t Module, oC_PortMan_Port_t Port)
returns true if port is reserved
Definition: oc_portman.c:567
bool oC_Tcp_Server_IsCorrect(oC_Tcp_Server_t Server)
checks if the TCP server object is correct
oC_ErrorCode_t oC_Tcp_Send(oC_Tcp_Connection_t Connection, const void *Buffer, oC_MemorySize_t Size, oC_Time_t Timeout)
sends data by using TCP connection
Definition: oc_tcp.c:819
oC_ErrorCode_t oC_PortMan_UnregisterModule(oC_Module_t Module, oC_Time_t Timeout)
unregisters module in the port manager
Definition: oc_portman.c:328
oC_ErrorCode_t oC_Tcp_Server_Stop(oC_Tcp_Server_t Server)
stops the TCP server
stores configuration of the module
Definition: oc_portman.h:63
#define oC_uint16_MAX
maximum value for uint16_t type
Definition: oc_stdtypes.h:149
static oC_List(oC_Tcp_Server_t)
Definition: oc_tcp.c:67
stores TCP server data
Definition: oc_tcp_server.c:67
static bool oC_Module_TurnOffVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned off
Definition: oc_module.h:155
static const oC_Allocator_t Allocator
Definition: oc_eth.c:152
oC_ErrorCode_t oC_Tcp_Listen(const oC_Net_Address_t *Source, oC_Tcp_Server_t *outServer, uint32_t MaxConnections, oC_Time_t Timeout)
starts a server that listen at the given address
Definition: oc_tcp.c:615
static bool oC_Module_TurnOnVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned on
Definition: oc_module.h:138
oC_ErrorCode_t oC_Tcp_Connect(const oC_Net_Address_t *Destination, oC_Tcp_Port_t LocalPort, oC_Tcp_Connection_t *outConnection, oC_Time_t Timeout)
connects to the remote TCP server
Definition: oc_tcp.c:443
static oC_ErrorCode_t CloseConnectionsCreatedBy(oC_Process_t Process, oC_Time_t Timeout)
closes all connections that has been created by the given process
Definition: oc_tcp.c:973
static oC_ErrorCode_t CloseServersCreatedBy(oC_Process_t Process, oC_Time_t Timeout)
closes all servers that has been created by the given process
Definition: oc_tcp.c:947
oC_ErrorCode_t oC_Tcp_ReleasePort(oC_Tcp_Port_t Port, oC_Time_t Timeout)
releases TCP port
Definition: oc_tcp.c:371
oC_ErrorCode_t oC_PortMan_ReservePort(oC_Module_t Module, oC_PortMan_Port_t *Port, oC_Time_t Timeout)
reserves a port of the given module
Definition: oc_portman.c:389
oC_ErrorCode_t oC_Tcp_TurnOff(oC_Time_t Timeout)
turns off TCP module
Definition: oc_tcp.c:185
oC_ErrorCode_t oC_PortMan_ReleasePort(oC_Module_t Module, oC_PortMan_Port_t Port, oC_Time_t Timeout)
releases port
Definition: oc_portman.c:457
oC_ErrorCode_t oC_Tcp_Receive(oC_Tcp_Connection_t Connection, void *outBuffer, oC_MemorySize_t Size, oC_Time_t Timeout)
receives data by using TCP connection
Definition: oc_tcp.c:858
oC_Net_AddressType_t Type
Type of the address stored inside.
Definition: oc_net.h:443
oC_Net_Port_t Port
Port of the address.
Definition: oc_net.h:450
bool oC_Tcp_Server_Delete(oC_Tcp_Server_t *Server, oC_Time_t Timeout)
deletes TCP server
#define NULL
pointer to a zero
Definition: oc_null.h:37
static void oC_Module_TurnOff(oC_Module_t Module)
sets module as turned off
Definition: oc_module.h:185