Beispiel #1
0
		public NetState(Socket socket, SocketConnector messagePump) {
			mSocket = socket;
			mBuffer = new ByteQueue();
			Seeded = false;
			mRunning = false;
			mRecvBuffer = new byte[BufferSize];
			mMessagePump = messagePump;

			mSendQueue = new SendQueue();
			UpdateAcitivty();

			mInstances.Add(this);

			try {
				mAddress = ((IPEndPoint)mSocket.RemoteEndPoint).Address.Intern();
				mToString = mAddress.ToString();
			} catch (Exception ex) {
				throw;
			}

			mConnectedOn = DateTime.Now;

			if (mCreatedCallback != null) {
				mCreatedCallback(this);
			}
		}
Beispiel #2
0
        public NetState(Socket socket, SocketConnector messagePump)
        {
            mSocket      = socket;
            mBuffer      = new ByteQueue();
            Seeded       = false;
            mRunning     = false;
            mRecvBuffer  = new byte[BufferSize];
            mMessagePump = messagePump;

            mSendQueue = new SendQueue();
            UpdateAcitivty();

            mInstances.Add(this);

            try {
                mAddress  = ((IPEndPoint)mSocket.RemoteEndPoint).Address.Intern();
                mToString = mAddress.ToString();
            } catch (Exception ex) {
                throw;
            }

            mConnectedOn = DateTime.Now;

            if (mCreatedCallback != null)
            {
                mCreatedCallback(this);
            }
        }
Beispiel #3
0
		public SocketListener(IPEndPoint ipep, SocketConnector connector) {
			mAccepted = new Queue<Socket>();
			mAcceptedSyncRoot = ((ICollection)mAccepted).SyncRoot;
			mOnAccept = OnAccept;
			mListener = Bind(ipep, connector);
			if (mListener == null) {
				throw new Exception("Could not bind IP to Socket! Please check your Network Configuration!");
			}
		}
Beispiel #4
0
 public SocketListener(IPEndPoint ipep, SocketConnector connector)
 {
     mAccepted         = new Queue <Socket>();
     mAcceptedSyncRoot = ((ICollection)mAccepted).SyncRoot;
     mOnAccept         = OnAccept;
     mListener         = Bind(ipep, connector);
     if (mListener == null)
     {
         throw new Exception("Could not bind IP to Socket! Please check your Network Configuration!");
     }
 }
Beispiel #5
0
		protected Socket Bind(IPEndPoint ipep, SocketConnector connector) {
			Socket s = SocketPool.AcquireSocket();

			try {
				s.LingerState.Enabled = false;
				s.ExclusiveAddressUse = false;

				s.Bind(ipep);
				s.Listen(8);

				if (ipep.Address.Equals(IPAddress.Any)) {
					try {
						CConsole.StatusLine(String.Format("start listen on {0}:{1}", IPAddress.Loopback, ipep.Port));

						IPHostEntry iphe = Dns.GetHostEntry(Dns.GetHostName());
						IPAddress[] ipList = iphe.AddressList;
						foreach (IPAddress ip in ipList) {
							CConsole.StatusLine(String.Format("# {0}:{1}", ip, ipep.Port));
						}
					} catch { }
				} else {
					if (ipep.Address.ToString() != connector.IP) {
						CConsole.StatusLine(String.Format("start listen on {0} -> {1}:{2}", connector.IP, ipep.Address, ipep.Port));
					} else {
						CConsole.StatusLine(String.Format("start listen on {0}:{1}", ipep.Address, ipep.Port));
					}
				}

				IAsyncResult res = s.BeginAccept(SocketPool.AcquireSocket(), 0, mOnAccept, s);
				return s;
			} catch (Exception e) {
				/* TODO
				 * throws more Exceptions like this
				 */
				var se = e as SocketException;
				if (se != null) {
					if (se.ErrorCode == 10048) {
						// WSAEADDRINUSE
						CConsole.ErrorLine(String.Format("Listener Failed: {0} -> {1}:{2} (In Use)", connector.IP, ipep.Address, ipep.Port));
					} else if (se.ErrorCode == 10049) {
						// WSAEADDRNOTAVAIL
						CConsole.ErrorLine(String.Format("Listener Failed: {0} -> {1}:{2} (Unavailable)", connector.IP, ipep.Address, ipep.Port));
					} else {
						CConsole.ErrorLine("Listener Exception:");
						CConsole.WriteLine(e);
					}
				}

				return null;
			}
		}
Beispiel #6
0
        protected Socket Bind(IPEndPoint ipep, SocketConnector connector)
        {
            Socket s = SocketPool.AcquireSocket();

            try {
                s.LingerState.Enabled = false;
                s.ExclusiveAddressUse = false;

                s.Bind(ipep);
                s.Listen(8);

                if (ipep.Address.Equals(IPAddress.Any))
                {
                    try {
                        CConsole.StatusLine(String.Format("start listen on {0}:{1}", IPAddress.Loopback, ipep.Port));

                        IPHostEntry iphe   = Dns.GetHostEntry(Dns.GetHostName());
                        IPAddress[] ipList = iphe.AddressList;
                        foreach (IPAddress ip in ipList)
                        {
                            CConsole.StatusLine(String.Format("# {0}:{1}", ip, ipep.Port));
                        }
                    } catch { }
                }
                else
                {
                    if (ipep.Address.ToString() != connector.IP)
                    {
                        CConsole.StatusLine(String.Format("start listen on {0} -> {1}:{2}", connector.IP, ipep.Address, ipep.Port));
                    }
                    else
                    {
                        CConsole.StatusLine(String.Format("start listen on {0}:{1}", ipep.Address, ipep.Port));
                    }
                }

                IAsyncResult res = s.BeginAccept(SocketPool.AcquireSocket(), 0, mOnAccept, s);
                return(s);
            } catch (Exception e) {
                /* TODO
                 * throws more Exceptions like this
                 */
                var se = e as SocketException;
                if (se != null)
                {
                    if (se.ErrorCode == 10048)
                    {
                        // WSAEADDRINUSE
                        CConsole.ErrorLine(String.Format("Listener Failed: {0} -> {1}:{2} (In Use)", connector.IP, ipep.Address, ipep.Port));
                    }
                    else if (se.ErrorCode == 10049)
                    {
                        // WSAEADDRNOTAVAIL
                        CConsole.ErrorLine(String.Format("Listener Failed: {0} -> {1}:{2} (Unavailable)", connector.IP, ipep.Address, ipep.Port));
                    }
                    else
                    {
                        CConsole.ErrorLine("Listener Exception:");
                        CConsole.WriteLine(e);
                    }
                }

                return(null);
            }
        }