Example #1
0
 public static ISocket TcpAccept(ISocket listenSocket, SocketCommunicationTypes type = SocketCommunicationTypes.Blocking, Func <ISocket, Exception, Socket> callback = null)
 {
     if (type == SocketCommunicationTypes.Blocking)
     {
         return(AwesomeSocket.New(listenSocket.GetSocket().Accept()));
     }
     if (callback == null)
     {
         throw new ArgumentNullException(string.Format("{0}=null; You must provide a valid callback when using the NonBlocking type", "callback"));
     }
     new Thread(() => TcpAcceptThread(listenSocket, callback)).Start();
     return(null);
 }
Example #2
0
        public static ISocket TcpConnect(string ipAddress, int port, SocketCommunicationTypes type = SocketCommunicationTypes.Blocking, Func <ISocket, Exception, Socket> callback = null)
        {
            var connectSocket  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var ip             = IPAddress.Parse(ipAddress);
            var remoteEndpoint = new IPEndPoint(ip, port);

            if (type == SocketCommunicationTypes.Blocking)
            {
                connectSocket.Connect(remoteEndpoint);
                return(AwesomeSocket.New(connectSocket));
            }
            if (callback == null)
            {
                throw new ArgumentNullException(string.Format("{0}=null; You must provide a valid callback when using the NonBlocking type", "callback"));
            }
            new Thread(() => TcpConnectThread(AwesomeSocket.New(connectSocket), remoteEndpoint, callback)).Start();
            return(null);
        }
Example #3
0
 public static Tuple<int, EndPoint> ReceiveMessage(ISocket socket, Buffer buffer, SocketCommunicationTypes type = SocketCommunicationTypes.Blocking, MessageThreadCallback callback = null)
 {
     if (type == SocketCommunicationTypes.Blocking)
     {
         switch (socket.GetSocket().ProtocolType)
         {
             case ProtocolType.Tcp:
                 return Tuple.Create(socket.GetSocket().Receive(Buffer.GetBufferRef(buffer)), socket.GetSocket().RemoteEndPoint);
             case ProtocolType.Udp:
                 EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                 return Tuple.Create(socket.GetSocket().ReceiveFrom(Buffer.GetBufferRef(buffer), ref remoteEndPoint), remoteEndPoint);
             default:
                 throw new ConstraintException("Socket must be of type TCP or UDP.");
         }
     }
     if (callback == null) throw new ArgumentNullException(string.Format("{0}=null; You must provide a valid callback when using the NonBlocking type", "callback"));
     new Thread(() => MessageReceiveThread(socket, buffer, callback)).Start();
     return Tuple.Create(-1, new IPEndPoint(-1, -1) as EndPoint);  //Return negative 1 as 0 bytes received is valid and we want an invalid value
 }
Example #4
0
        public static Tuple <int, EndPoint> ReceiveMessage(ISocket socket, Buffer buffer, SocketCommunicationTypes type = SocketCommunicationTypes.Blocking, MessageThreadCallback callback = null)
        {
            if (type == SocketCommunicationTypes.Blocking)
            {
                switch (socket.GetSocket().ProtocolType)
                {
                case ProtocolType.Tcp:
                    return(Tuple.Create(socket.GetSocket().Receive(Buffer.GetBufferRef(buffer)), socket.GetSocket().RemoteEndPoint));

                case ProtocolType.Udp:
                    EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    return(Tuple.Create(socket.GetSocket().ReceiveFrom(Buffer.GetBufferRef(buffer), ref remoteEndPoint), remoteEndPoint));

                default:
                    throw new ConstraintException("Socket must be of type TCP or UDP.");
                }
            }
            if (callback == null)
            {
                throw new ArgumentNullException(string.Format("{0}=null; You must provide a valid callback when using the NonBlocking type", "callback"));
            }
            new Thread(() => MessageReceiveThread(socket, buffer, callback)).Start();
            return(Tuple.Create(-1, new IPEndPoint(-1, -1) as EndPoint));  //Return negative 1 as 0 bytes received is valid and we want an invalid value
        }
Example #5
0
 public static ISocket TcpConnect(string ipAddress, int port, SocketCommunicationTypes type = SocketCommunicationTypes.Blocking, Func<ISocket, Exception, Socket> callback = null)
 {
     var connectSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     var ip = IPAddress.Parse(ipAddress);
     var remoteEndpoint = new IPEndPoint(ip, port);
     if (type == SocketCommunicationTypes.Blocking)
     {
         connectSocket.Connect(remoteEndpoint);
         return AwesomeSocket.New(connectSocket);
     }
     if (callback == null) throw new ArgumentNullException(string.Format("{0}=null; You must provide a valid callback when using the NonBlocking type", "callback"));
     new Thread(() => TcpConnectThread(AwesomeSocket.New(connectSocket), remoteEndpoint, callback)).Start();
     return null;
 }
Example #6
0
 public static ISocket TcpAccept(ISocket listenSocket, SocketCommunicationTypes type = SocketCommunicationTypes.Blocking, Func<ISocket, Exception, Socket> callback = null)
 {
     if (type == SocketCommunicationTypes.Blocking)
     {
         return AwesomeSocket.New(listenSocket.GetSocket().Accept());
     }
     if (callback == null) throw new ArgumentNullException(string.Format("{0}=null; You must provide a valid callback when using the NonBlocking type", "callback"));
     new Thread(() => TcpAcceptThread(listenSocket, callback)).Start();
     return null;
 }