- Much work on the networking

- ServerSocket done.
- ClientSocket done.
- Few fixes required:
   - Warning in ServerSocket
   - Friends possible in C#?
This commit is contained in:
u8sand_cp 2010-07-19 07:09:34 -07:00
parent 6987d8178f
commit a26280b711
5 changed files with 228 additions and 69 deletions

View file

@ -74,7 +74,6 @@
<Compile Include="Networking\ClientSocket.cs" />
<Compile Include="Networking\Server.cs" />
<Compile Include="Networking\ServerSocket.cs" />
<Compile Include="Networking\Socket.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scripting\GameObject.cs" />
<Compile Include="Scripting\GameObjectCollection.cs" />

View file

@ -2,21 +2,69 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
// TODO: everything D:
// TODO: Make ClientSocket a friend of ServerSocket so I can make sock and type private
// other, havn't thought of what else I need
namespace MudEngine.Networking
{
class ClientSocket : Socket
class ClientSocket
{
public ClientSocket()
{
type = 0;
ip = 0;
}
~ClientSocket()
{
type = 0;
ip = 0;
}
public int Send(byte[] ba,int size,SocketFlags sf)
{
try
{
sock.Send(ba, size, sf);
}
catch (Exception)
{
return -1;
}
return 1;
}
public int Receive(ref byte[] ba, int size, SocketFlags sf)
{
try
{
sock.Receive(ba, size, sf);
}
catch (Exception)
{
return -1;
}
return 1;
}
public int CleanUp()
{
try
{
sock.Disconnect(true);
sock.Close();
sock.Dispose();
ip = 0;
type = 0;
}
catch (Exception)
{
return -1;
}
return 1;
}
public string ip;
public ProtocolType type;
public long ip;
public Socket sock;
}
}

View file

@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MudEngine.Networking;
// TODO: everything D:
@ -19,6 +18,7 @@ namespace MudEngine.Networking
}
private ServerSocket server;
//private ServerSocket server;
//private ClientSocket[] clients;
}
}

View file

@ -2,17 +2,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
// TODO: everything D:
// TODO: ln 167 warning, catch more errors and don't return all of em as unknown :o
namespace MudEngine.Networking
{
enum SocketType
{
TCP = 1,
UDP = 2
};
class ServerSocket : Socket
class ServerSocket
{
public ServerSocket()
{
@ -27,55 +24,191 @@ namespace MudEngine.Networking
stage = 0;
type = 0;
}
// all methods return int, 1 = success, -1 = error, > 1 are just extra info w/ success
// < -1, known error find it out with: (string)getError(er_code);
public int Initialize(int p,ProtocolType pt)
{
try
{
if (stage == 0)
{
if (pt != ProtocolType.Tcp && pt != ProtocolType.Udp)
return -3;
port = p;
type = pt;
stage++;
}
else
return -2;
}
catch (Exception)
{
return -1;
}
return 1;
}
public int Start()
{
try
{
if (stage == 1)
{
if (type == ProtocolType.Tcp)
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, type);
else
sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, type);
stage++;
}
else
return -2;
}
catch (Exception)
{
return -1;
}
return 1;
}
public int Bind()
{
try
{
if (stage == 2)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);
sock.Bind(ipep);
stage++;
}
else
return -2;
}
catch (Exception)
{
return -1;
}
return 1;
}
public int Listen()
{
try
{
if (stage == 3 && type == ProtocolType.Udp)
return -4;
if (stage == 3)
{
sock.Listen(10);
stage++;
}
else
return -2;
}
catch (Exception)
{
return -1;
}
return 1;
}
public int Accept(ref ClientSocket cs)
{
try
{
if (type == ProtocolType.Udp)
return -2;
if (stage == 4)
stage++;
public int init(int p, SocketType st)
{
return 0;
if (stage == 5)
{
cs.sock = sock.Accept();
cs.type = ProtocolType.Tcp;
}
else
return -2;
}
catch (Exception)
{
return -1;
}
return 1;
}
public int start()
public int SendTo(byte[] ba, int size, SocketFlags sf, ref ClientSocket rcs)
{
return 0;
try
{
if (type != ProtocolType.Udp)
return -3;
if (stage == 3)
stage += 2;
if (stage == 5)
{
IPEndPoint ipep = new IPEndPoint(rcs.ip, port);
sock.SendTo(ba, size, sf, ipep);
}
else
return -2;
}
catch (Exception)
{
return -1;
}
return 1;
}
public int bind()
public int ReceiveFrom(ref byte[] ba, int size, SocketFlags sf, ref ClientSocket rcs)
{
return 0;
try
{
if (type != ProtocolType.Udp)
return -3;
if (stage == 3)
stage += 2;
if (stage == 5)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
EndPoint ep = (EndPoint)ipep;
sock.ReceiveFrom(ba, size, sf, ref ep);
rcs.ip = ipep.Address.Address; // Why am I getting this warning?
}
else
return -2;
}
catch (Exception)
{
return -1;
}
return 1;
}
public int listen()
public int CleanUp()
{
return 0;
try
{
sock.Close();
sock.Dispose();
}
catch (Exception)
{
return -1;
}
return 1;
}
public int accept()
{
return 0;
}
public int send()
{
return 0;
}
public int recv()
{
return 0;
}
public int end()
{
return 0;
}
public string getError(int er_code)
public string GetError(int er_code)
{
if (er_code > 0)
return "No Error";
switch (er_code)
{
default:
return "Unknown Error";
case -2:
return "Method cannot be called yet.";
case -3:
return "ProtocolType not supported.";
case -4:
return "The ProtocolType does not support that method.";
default:
return "Unknown Error";
}
}
private int stage;
private int port;
private SocketType type;
public int stage { get; private set; }
public int port { get; private set; }
public ProtocolType type { get; private set; }
private Socket sock;
}
}

View file

@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// TODO: everything D:
namespace MudEngine.Networking
{
class Socket
{
public Socket()
{
}
~Socket()
{
}
}
}